﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Company.Hardware.Base;
using System.Text.Json.Serialization;
using Prism.Modularity;
using Company.Core.Models;
using Company.Database.Base.Entities;

namespace Company.Application.Share.Entities
{
    public class DeviceEntity
    {
        public int Id { get; set; }
        /// <summary>
        /// 设备名称
        /// </summary>
        public string? Name { get; set; }
        /// <summary>
        /// 所属医院ID
        /// </summary>
        public int HospitalId { get; set; }
        /// <summary>
        /// 所属工作站ID
        /// </summary>
        public int StationId { get; set; }
        /// <summary>
        /// 设备编码
        /// </summary>
        public string? Code { get; set; }
        /// <summary>
        /// 设备型号
        /// </summary>
        public string? Model { get; set; }
        /// <summary>
        /// 设备供应商
        /// </summary>
        public string? Supplier { get; set; }
        /// <summary>
        /// 通讯协议：串口，网口（服务器或客户端）
        /// </summary>
        public int Protocol { get; set; }
        public string? IpAddress { get; set; }
        public int Port { get; set; }
        public string? SerialName { get; set; }
        public int BaudRate { get; set; }
        /// <summary>
        /// 解码器
        /// </summary>
        public string? ClassName { get; set; }
        /// <summary>
        /// 0=启用|1=停用
        /// </summary>
        public int Status { get; set; }

        [JsonIgnore]
        public IDecoder? Decoder { get; private set; }
        [JsonIgnore]
        public IModuleManager? ModuleManager { get; private set; }
        [JsonIgnore]
        public IUploader? Uploader { get; private set; }

        /// <summary>
        /// 添加解码器
        /// </summary>
        /// <param name="decoder"></param>
        public void Add(IDecoder decoder)
        {
            if (decoder != null)
            {
                Decoder ??= decoder;
                Decoder.OnUpload += async (data) =>
                {
                    if (Uploader != null)
                    {
                        if (data.Source == null ||
                        data.HL7MSH == null ||
                        data.HL7PID == null ||
                        data.HL7OBR == null ||
                        data.HL7OBXList == null)
                            return;

                        //封装基础数据
                        string guid = Guid.NewGuid().ToString();
                        data.Source.GUID= guid;
                        data.HL7MSH.GUID= guid;
                        data.HL7PID.GUID= guid;
                        data.HL7OBR.GUID= guid;
                        foreach (var item in data.HL7OBXList)
                        {
                            item.GUID = guid;
                        }

                        data.Source.DeviceId = Id;
                        data.HL7MSH.DeviceId = Id;
                        data.HL7PID.DeviceId = Id;
                        data.HL7OBR.DeviceId = Id;
                        foreach (var item in data.HL7OBXList)
                        {
                            item.DeviceId = Id;
                        }

                        data.Source.StationId = StationId;
                        data.HL7MSH.StationId = StationId;
                        data.HL7PID.StationId = StationId;
                        data.HL7OBR.StationId = StationId;
                        foreach (var item in data.HL7OBXList)
                        {
                            item.StationId = StationId;
                        }

                        data.Source.HospitalId = HospitalId;
                        data.HL7MSH.HospitalId = HospitalId;
                        data.HL7PID.HospitalId = HospitalId;
                        data.HL7OBR.HospitalId = HospitalId;
                        foreach (var item in data.HL7OBXList)
                        {
                            item.HospitalId = HospitalId;
                        }

                        await Uploader.UploadAsync<Result<HL7>>(data);
                    }
                };
            }
        }

        /// <summary>
        /// 添加模块管理器
        /// </summary>
        /// <param name="moduleManager"></param>
        public void Add(IModuleManager moduleManager)
        {
            ModuleManager ??= moduleManager;
        }

        /// <summary>
        /// 添加上传数据服务类
        /// 添加上传数据服务类
        /// </summary>
        /// <param name="uploader"></param>
        public void Add(IUploader uploader) => Uploader ??= uploader;
    }
}
